package com.weixin.base.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.weixin.base.service.TpService;
import com.weixin.base.util.Tools;

@Controller
public class TpAction {

	@Autowired
	private TpService tpService;

	/**
	 * 返回首页 nWxTemplateId 首页模版ID
	 */
	@RequestMapping(value = "/tpmain.do")
	public String tpmain(HttpServletRequest request, HttpServletResponse response) {
		int userId = Tools.convertRi(request.getParameter("userId"));
		
		int nTemplateId = tpService.selectTemplateId(userId);

		List<Map<String, Object>> slideList = tpService.selectTbWxMenu(userId, 1);
		List<Map<String, Object>> menuList = tpService.selectTbWxMenu(userId, 2);

		request.setAttribute("userid", userId);
		request.setAttribute("nTemplateId", nTemplateId);
		request.setAttribute("slideList", slideList);
		request.setAttribute("menuList", menuList);

		if (nTemplateId == 1) {
			return "/tp/tpmain/tpmain";
		}else if(nTemplateId == 2){
			return "/tp/tpmain/tpmain2";
		} else {
			return "/404";
		}
	}

	/**
	 * 返回文章页 srandom 文章随机码
	 */
	@RequestMapping(value = "/tptext.do")
	public String tptext(HttpServletRequest request, HttpServletResponse response) {
		int userId = Tools.convertRi(request.getParameter("userid"));
		String srandom = Tools.convertRs(request.getParameter("srandom"));

		String scontent = tpService.selectTbWxText(userId, srandom);

		request.setAttribute("userid", userId);
		request.setAttribute("srandom", srandom);
		request.setAttribute("scontent", scontent);
		
		if (scontent != null) {
			return "/tp/tptext/tptext";
		} else {
			return "/404";
		}
	}
}
